package cn.kimming.rpc.remoting.server;

import cn.kimming.rpc.common.annotation.RpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.HashMap;

/**
 * 继承了HashMap, 用于缓存服务实现
 * key: 服务接口全限定名
 * value: 服务实现对象
 */
public class ServiceMap extends HashMap<String, Object> {
	private static final Logger logger = LoggerFactory.getLogger(ServiceMap.class);

	public ServiceMap(Collection<Object> serviceImplList) {
		super(serviceImplList.size());
		extractRpcService2Map(serviceImplList);
	}

	/**
	 * 从服务实现类中提取@RpcService注解及其属性, 放入到map中
	 */
	private void extractRpcService2Map(Collection<Object> serviceImplList) {
		for (Object serviceImpl : serviceImplList) {
			RpcService annotation = serviceImpl.getClass().getAnnotation(RpcService.class);
			String interfaceName = annotation.value().getCanonicalName();
			logger.info("[my-rpc] put {}, impl: {} into serviceMap" + interfaceName, serviceImpl);
			put(interfaceName, serviceImpl);
		}
	}

}
